local DIY = require "packages/diy_utility/diy_utility"

local shencai = fk.CreateSkill{
  name = "lvshi__shencai",
}

Fk:loadTranslationTable{
  ["lvshi__shencai"] = "神裁",
  [":lvshi__shencai"] = "出牌阶段，你可移去中央区的一张【杀】视为无次数限制使用之，目标角色抵消此【闪】的方式改为移去中央区的一张【闪】。",
  ["#lvshi__shencai"] = "你可以选择中央区内的一张【杀】和目标角色",
  ["#lvshi__shencai-jink"] = "你可移去中央区的一张【闪】抵消此【杀】",
  ["#ls__shencai_response"] = "%from 移去了中央区的 %arg",

  ["$lvshi__shencai1"] = "汝罪之大，似彻天之山、盈渊之海!",
  ["$lvshi__shencai2"] = "生犯贪嗔痴戾疑，死受鞭笞斧灼烹！",
}

---@param room AbstractRoom
---@param names string[]
---@return integer[]
local getCenter = function (room, names)
  return table.filter(DIY.getCenterCards(room), function (id)
    return table.contains(names, Fk:getCardById(id).trueName)
  end)
end

shencai:addAcquireEffect(function (self, player, is_start)
  player.room:addSkill("#CenterArea")
end)

shencai:addEffect("viewas", {
  prompt = "#lvshi__shencai",
  pattern = "slash",
  expand_pile = function (self, player)
    return getCenter(Fk:currentRoom(), {"slash"})
  end,
  card_filter = function (self, player, to_select, selected)
    return #selected == 0 and table.contains(getCenter(Fk:currentRoom(), {"slash"}), to_select)
  end,
  view_as = function (self, player, cards)
    if #cards ~= 1 then return end
    local card = Fk:cloneCard(Fk:getCardById(cards[1]).name)
    card.skillName = shencai.name
    card:addFakeSubcards(cards)
    --self.cost_data = cards
    return card
  end,
  before_use = function(self, player, use)
    use.extraUse = true
    DIY.removeCenterCards(player, use.card.fake_subcards)
  end,
  enabled_at_play = function (self, player)
    return #getCenter(Fk:currentRoom(), {"slash"}) > 0
  end,
  enabled_at_response = Util.FalseFunc,
})

shencai:addEffect("targetmod", {
  bypass_times = function(self, player, skill, scope, card, to)
    return card and table.contains(card.skillNames, shencai.name)
  end,
})

shencai:addEffect(fk.PreCardEffect, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(shencai.name) and data.card.skillName == shencai.name
    and not (data.unoffsetable or data:isUnoffsetable(data.to))
    and not (data.disresponsive or data:isDisresponsive(data.to))
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local jinks = getCenter(room, {"jink"})
    if #jinks > 0 then
      local id = room:askToChooseCards(data.to,
      {target = data.to,
      max = 1,
      min = 0,
      skill_name = shencai.name,
      prompt = "#lvshi__shencai-jink",
      flag = { card_data = {{"lvshi__shencai", jinks}} },
      })
      if #id > 0 then
        DIY.removeCenterCards(target, id)
        room:sendLog{
          type = "#ls__shencai_response",
          from = data.to.id,
          arg = Fk:getCardById(id[1]):toLogString(),
          toast = true,
        }
        data.isCancellOut = true
      else
        data:setUnoffsetable(data.to)
      end
    else
      data:setUnoffsetable(data.to)
    end 
  end,
})

return shencai